#include "nameMenu.h"

//debuging------------------------------
//#define NAME_MENU_DEBUG
#ifdef NAME_MENU_DEBUG
#include <gtkmm/main.h>
#include <gtkmm/window.h>
#include <iostream>
int main(int argc, char* argv[]){
	Gtk::Main kit(argc, argv);
	Gtk::Window scr;
	scr.resize(600, 300);
	scr.set_border_width(20);
        
	nameMenu* nmu = new nameMenu();
	nmu->addNames("HI");
	nmu->addNames("zim");
	nmu->addNames("cunsacks");
	nmu->addNames("yyyy");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("cunsacks");
	nmu->addNames("yo");
	scr.add(*nmu);
	nmu->show();
	Gtk::Main::run(scr);
}
#endif
//---------------------------------------
//nameMenu--------------------------------------------------
nameMenu::nameMenu(string** chce){
	set_can_focus();
	offset = 0;
	choice = 0;
}
nameMenu::~nameMenu(){
}
void nameMenu::addNames(string newstr){
	names.push_back(new string(newstr));
}
bool nameMenu::on_key_press_event(GdkEventKey* event){
	switch(event->keyval){
		case GDK_Up:
		case GDK_Left:
			choice--;
			if(choice < 0)
				choice = names.size()-1;
			break;
		case GDK_Down:
		case GDK_Right:
			choice++;
			if(choice >= names.size())
				choice = 0;
			break;
	}
	draw();
	return true;
}
bool nameMenu::on_expose_event(GdkEventExpose *event){
	draw();
}
void nameMenu::keypress(GdkEventKey* event){
	on_key_press_event(event);
}
void nameMenu::draw(){
	//grab the window
	Glib::RefPtr<Gdk::Window> window = get_window();
	Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();

	if(window){ //window not null
		//save cairo state;
		cr->save();
		
		//window size info
		Gtk::Allocation allocation = get_allocation();
		const int width = allocation.get_width();
		const int height = allocation.get_height();
		
		//window locations
		int center_x = width/2;
		int center_y = height/2;
		int leftBind = 0 + LEFT_BORDER;
		int rightBind = width - RIGHT_BORDER;
		int topBind = 0 + TOP_BORDER;
		int botBind = height - BOTTOM_BORDER;
		
		//font information
		cr->set_source_rgb(1, 1, 1); 
		cr->select_font_face("monospace", Cairo::FONT_SLANT_NORMAL, Cairo::FONT_WEIGHT_BOLD);
		Cairo::TextExtents sizeing;
		cr->get_text_extents("TEST", sizeing);
		int fontSpacing = sizeing.height * 2;
		int strsToPrint = MIN((botBind - topBind) / fontSpacing, names.size());
		
		//draw background
		cr->save();
		cr->set_fill_rule(Cairo::FILL_RULE_EVEN_ODD);
		cr->set_source_rgb(1, 0, 0);
		cr->rectangle(0, 0, width, height);
		cr->fill();
		cr->restore();
		//draw background
		cr->save();
		cr->set_fill_rule(Cairo::FILL_RULE_WINDING);
		cr->set_source_rgb(0, 1, 0);
		cr->rectangle(leftBind, topBind, rightBind - leftBind, botBind - topBind);
		cr->fill();
		cr->restore();
		
		//start drawing strings
		int line = topBind + fontSpacing;
		for(int i = offset; i < strsToPrint; i++){
			//prepare information
			cr->get_text_extents(*names[i], sizeing);
			//draw selection bar
			if(i == choice){
				cr->save();
				cr->move_to(leftBind, line);
				cr->set_source_rgb(0, 0, 1);
				cr->rectangle(leftBind, line - sizeing.height - 2, rightBind - leftBind, sizeing.height + 4);
				cr->fill();
				cr->restore();
			}
			cr->move_to(leftBind + 20, line);
			cr->show_text(*names[i]);
			line += fontSpacing;
		}
		
		//restore cairo state
		cr->restore();
	}
}
//----------------------------------------------------------
